package com.gmzg.gmzgoffice.contacts.adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.gmzg.gmzgoffice.R;
import com.gmzg.gmzgoffice.contacts.model.ContactItem;

import java.util.List;




public class ContactListAdapter extends BaseAdapter {
    private Context mContext;

    private List<ContactItem> mItems;

    public ContactListAdapter(Context context, List<ContactItem> items) {
        mContext = context;
        mItems = items;
    }

    @Override
    public int getCount() {
        int ret = 0;
        if (mItems != null) {
            ret = mItems.size();
        }
        return ret;
    }

    @Override
    public Object getItem(int position) {
        return mItems.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View ret = null;

        if (convertView != null) {
            ret = convertView;
        }else{
            LayoutInflater inflater = LayoutInflater.from(mContext);
            ret = inflater.inflate(R.layout.item_contact, parent, false);
        }

        ViewHolder holder = (ViewHolder) ret.getTag();
        if (holder == null) {
            holder = new ViewHolder(ret);
            ret.setTag(holder);
        }

        ContactItem contactItem = mItems.get(position);
        holder.bindView(contactItem);

        return ret;
    }

    private static class ViewHolder {

        private TextView mTextName;
        private TextView mTextCount;

        ViewHolder(View itemView){
            mTextName = (TextView) itemView.findViewById(R.id.item_contact_name);
        }

        void bindView(ContactItem contactItem) {
            mTextName.setText(contactItem.getName());
        }
    }
}
